//
//  NetworkClient+Settings.swift
//  SitPosition
//
//  Created by apple on 2019/2/7.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import Alamofire


/**
 * 用户的学习配置接口
 */
extension NetworkApi {
    
    // 查询用户的学习配置
    public static func queryUserConfigs(_ userId: String) -> NetworkApi {
        let placeholder: Placeholder = [
            "id": userId
        ]
        return NetworkApi("/userCenter/userConfigs/{id}", method: .get, placeholder: placeholder)
    }
    
    // 修改用户的学习配置
    public static func updateUserConfigs(_ userId: String) -> NetworkApi {
        let placeholder: Placeholder = [
            "id": userId
        ]
        return NetworkApi("/userCenter/userConfigs/{id}", method: .patch, placeholder: placeholder)
    }
}

extension NetworkClient {
    
    // 查询用户的学习配置
    func queryUserConfigs(userId: String,
                    success: DataParamCallback?,
                    failure: ErrorCallback?) {
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.queryUserConfigs(userId), params: nil, headers: nil)
            .responseData(completionHandler: completionHandler)
    }
    
    // 修改用户的学习配置
    func updateUserConfigs(userId: String,
                           userConfigs: UserConfigs,
                           success: DataParamCallback?,
                           failure: ErrorCallback?) {
        var params: Parameters = Parameters()
        params["id"] = userConfigs.id
        if let value_ = userConfigs.angleValue {
            params["angleValue"] = value_
        }
        if let value_ = userConfigs.studyTime {
            params["studyTime"] = value_
        }
        if let value_ = userConfigs.blackScreenTime {
            params["blackScreenTime"] = value_
        }
        if let value_ = userConfigs.alarmTime {
            params["alarmTime"] = value_
        }
        if let value_ = userConfigs.departureTime {
            params["departureTime"] = value_
        }
        if let value_ = userConfigs.ttsSound {
            params["ttsSound"] = value_
        }
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.updateUserConfigs(userId), params: params, headers: nil)
            .responseData(completionHandler: completionHandler)
    }
}
